package slibrary;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.struts2.ServletActionContext;

public class Slibrary extends ActionSupport {
    public String username = null;
    public Statement statement = null;
    public DBConnection user = null;
    public ResultSet resultSet = null;
    public String deletesql = null;
    public String addsql = null;
    public Slibrary() {
    }

    public String getData() throws Exception {
        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

//        String sql = "SELECT currenttable FROM [Sublibrary] where supiortable='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        String sql = "SELECT currenttable FROM [Sublibrary] where supiortable='" + this.username + "'";

        System.out.println(sql);

        try {
            this.resultSet = this.statement.executeQuery(sql);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        String user_name = null;
        String data = "user";

        try {
            while(this.resultSet.next()) {
                user_name = this.resultSet.getString(1);
                data = data + ":";
                data = data + user_name;
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            writer.write("error");
            return null;
        }

        if(data == "user") {
            writer.write("");
            return null;
        } else {
            data = data.replace("user:", "");
            System.out.println(data);
            writer.write(data);
            return null;
        }
    }
    /*public String subdatafind() throws Exception {
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        String data=null;
        String subdata=null;
        int max=0;
        if(username=="底栖生物名录")
        {
            username="Ben_ClassInform";
            max=30;
        }
        else if(username=="鱼名录")
        {
            username="FishClassInform";
            max=36;
        }
        else if(username=="浮游植物名录")
        {
            username="Phy_ClassInform";
            max=36;
        }
        else if(username=="浮游动物名录")
        {
            username="Zooplan_ClassInform";
            max=30;
        }
        else if(username=="监测指标")
        {
            username="MonIndex";
            max=4;
        }
        else if(username=="监测方法")
        {
            username="MonMethod";
            max=7;
        }
        String sql="Select * FROM ["+username+"]";
        System.out.println(sql);
        try {
            this.resultSet = this.statement.executeQuery(sql);
        } catch (Exception var11) {
            System.out.println("55558");
            var11.printStackTrace();
        }
        try {
            while(this.resultSet.next()) {
                for(int i=0;i<=max;i++) {
                    subdata = this.resultSet.getString(i);
                    data = data + ",";
                    data = data + subdata;
                }
                data=data + "?";
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            writer.write("error");
            System.out.println("555556");
            return null;
        }
        System.out.println("55555"+data);
        writer.write(data);
        return null;
    }*/
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

  /*  public String userQuery() throws Exception {
        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var13) {
            var13.printStackTrace();
        }

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        String sql = "SELECT * FROM [Sublibrary] where currenttable='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        System.out.println(sql);

        try {
            this.resultSet = this.statement.executeQuery(sql);
        } catch (Exception var11) {
            var11.printStackTrace();
        }

        String data = "";

        try {
            while(this.resultSet.next()) {
                String user_name = this.resultSet.getString(2) + ":";
                String password = this.resultSet.getString(3) + ":";
                String TelePhone = this.resultSet.getString(4) + ":";
                String Email = this.resultSet.getString(5) + ":";
                String Description = this.resultSet.getString(6) + ":";
                String superior = this.resultSet.getString(8) + ":";
                String roleName = this.resultSet.getString(9);
                data = data + user_name;
                data = data + password;
                data = data + TelePhone;
                data = data + Email;
                data = data + Description;
                data = data + superior;
                data = data + roleName;
            }
        } catch (Exception var14) {
            var14.printStackTrace();
            writer.write("error");
            return null;
        }

        System.out.println(data);
        data = data.replace("user;", "");
        writer.write(data);
        return null;
    }*/
    public String addsubdata() throws Exception{

        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var6) {
            var6.printStackTrace();
        }
        String sql = addsql;
        int result = 0;

        System.out.println(sql);

        try {
            result = this.statement.executeUpdate(sql);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if(result >= 1) {
            writer.write("success");
        } else {
            writer.write("failure");
        }

        return null;
    }
    public String deleteQuery() throws Exception {
        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        int result = 0;
        String sql = deletesql;
        System.out.println(sql);

        try {
            result = this.statement.executeUpdate(sql);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if(result >= 1) {
            writer.write("success");
        } else {
            writer.write("failure");
        }

        return null;
    }

    /*public String getidentify() throws IOException, SQLException {
        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        ResultSet result = null;
        String sql = "select roleName FROM [Sublibrary] where currenttable='" + this.username + "'";
        System.out.println(sql);

        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if(result.next()) {
            String identify = result.getString("roleName");
            System.out.println(identify);
            writer.write(identify);
        }

        return null;
    }

    public String idQuery() throws IOException, SQLException {
        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        ResultSet result = null;
        String sql = "select id FROM [Sublibrary] where currenttable='" + this.username + "'";
        System.out.println(sql);

        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if(result.next()) {
            String identify = result.getString("id");
            System.out.println(identify);
            writer.write(identify);
        }

        return null;
    }

    public String activeQuery() throws IOException, SQLException {

        try {
            this.user = DBConnection.getInstance();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception var6) {
            var6.printStackTrace();
        }
        if(this.username.equals("底栖生物名录"))
        {

        }
        ResultSet result = null;
        String sql = "select ID FROM [Sublibrary] where currenttable='" + this.username + "'";
        System.out.println(sql);

        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if(result.next()) {
            String identify = result.getString("active");
            System.out.println(identify);
            writer.write(identify);
        }

        return null;
    }

    public String newQuery() throws IOException
    {
        System.out.println("new");
        try{
            this.user= DBConnection.getInstance();//建立连接
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        int result=0;
        try
        {
            this.statement=this.user.getStatement();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String sql="INSERT INTO [Ben_ClassInform] (username,passwold,Telephone,Email,Description,active,superiorName,roleName)VALUES(null)";

        System.out.println(sql);
        try
        {
            result=this.statement.executeUpdate(sql);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(result==1)
        {
            writer.write("success");
        }
        else
        {
            writer.write("failure");
        }
        return null;
    }*/
}